package com.libinyn.base.i18n.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libinyn.base.i18n.config.i18n.I18nConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
 * 语言转换控制器
 * @fileName: I18nController.java
 * @author: bin.li01
 * @date: 2017/12/21 11:19 
 * @version: v1.0.0
 */
@Controller
public class I18nController {
    
    private static Logger logger = LoggerFactory.getLogger(I18nController.class);
    
    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("i18n",I18nConfig.getMessage("i18n",Locale.CHINA));
        modelAndView.setViewName("/main");
        return modelAndView;
    }

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request){
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("i18n",I18nConfig.getMessage("i18n",Locale.CHINA));
        modelAndView.setViewName("/main");
        return modelAndView;
    }
    
    /**
     * 中、英区域转换控制器
     * @param request
     * @param response
     * @param language
     * @return
     */
    @GetMapping("/changeLanguage/{language}")
    public ModelAndView  language(HttpServletRequest request, HttpServletResponse response,@PathVariable String language){
        Locale locale= request.getLocale();
        ModelAndView modelAndView = new ModelAndView();
        Locale aDefault = Locale.getDefault();
        //打印日志
        logger.debug("locale="+locale.toString());
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        logger.info("language:"+language);
        if(language==null||language.equals("")){
            return modelAndView;
        }else{
            if("CN".equals(language)){
                //会话区域解析器
                localeResolver.setLocale(request, response, Locale.CHINA);
                modelAndView.addObject("i18n",I18nConfig.getMessage("i18n",Locale.CHINA));
            }else if("US".equals(language)){
                localeResolver.setLocale(request, response, Locale.US);
                modelAndView.addObject("i18n",I18nConfig.getMessage("i18n",Locale.US));
            }else{
                //默认
                localeResolver.setLocale(request, response, Locale.CHINA);
                modelAndView.addObject("i18n",I18nConfig.getMessage("i18n",Locale.CHINA));
            }
        }
        modelAndView.setViewName("/main");
        return modelAndView;
    }
}